package py.una.pol.ii.is2.jdbc;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.sql.DriverManager;
import java.sql.*;
import java.sql.SQLException;

public class ConnectPg {
	private String connectionURL;
	private Connection connection = null;
	private String userName, passwrd, dbName;

	public ConnectPg(){
		connectionURL  = "jdbc:postgresql://localhost/";
		dbName = "sav";
		userName = "postgres";
		passwrd = "123456";
	}


	/**
	*	Establece una conexion JDBC con la base de datos postgres
	*/
	//URL jdbc:postgresql://{hostIP | hostName}:port[/database]
	public Connection getConnection(){
		// Leer drive de la database
		try{
			Class.forName("org.postgresql.Driver");
			// Get a Connection to the database
			connection = DriverManager.getConnection(connectionURL + dbName, userName, passwrd);
		}catch(SQLException e){
			System.out.println("SQL ERROR.");
			e.printStackTrace();
		}catch(ClassNotFoundException e){
			System.out.println("CLASS NOT FOUND ERROR.");
			e.printStackTrace();
		}
		return connection;
	}

	/**
	 * Cierra la conexion establecida con la base de datos
	 * @return void
	 * */
	public void cerrarConnection(Connection conexion){
        connection = conexion;
		try
		{
			connection.close();
		}catch(SQLException e){
			System.out.println("SQL ERROR");
			e.printStackTrace();
		}
	}

	public String getUserName() {
		return userName;
	}


	public void setUserName(String userName) {
		this.userName = userName;
	}


	public String getPasswrd() {
		return passwrd;
	}


	public void setPasswrd(String passwrd) {
		this.passwrd = passwrd;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

}